package jinyilw.swing.rlaf.ui.icon;

import java.awt.Component;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.Serializable;

import javax.swing.Icon;
import javax.swing.SwingConstants;
import javax.swing.plaf.UIResource;

import jinyilw.swing.ComponentTools;
import jinyilw.swing.graphics.GraphicsTools;
import jinyilw.swing.graphics.RenderingConfig;

public class RMenuArrowIcon implements Icon, UIResource, Serializable
{
	private static final long serialVersionUID = -7614343446018204362L;
	private int w, h;
	private RenderingConfig renderingConfig = new RenderingConfig();

	public RMenuArrowIcon()
	{
		w = 15;
		h = 15;
	}

	@Override
	public void paintIcon(Component c, Graphics g, int x, int y)
	{
		int size = Math.min(h / 2, w / 2);
		size = Math.max(size, 2);
		int direction = ComponentTools.isLeftToRight(c)
				? SwingConstants.EAST
				: SwingConstants.WEST;
		Graphics2D g2d = (Graphics2D) g;
		renderingConfig.setAAPainting(g2d);
		GraphicsTools.fillTriangle(g2d, x + (w - size) / 2, y + (h - size) / 2,
				size - 1, size, direction);
		renderingConfig.resetAAPainting(g2d);
	}

	@Override
	public int getIconWidth()
	{
		return w;
	}

	@Override
	public int getIconHeight()
	{
		return h;
	}
}
